<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 搜索表单 -->
      <a-form :label-col="{ md: { span: 6 }, sm: { span: 24 } }" :wrapper-col="{ md: { span: 18 }, sm: { span: 24 } }">
        <a-row>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="名称">
              <a-input v-model:value.trim="settingsWhere.name" placeholder="请输入" 
                allow-clear />
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="类型">
              <a-select v-model:value="settingsWhere.typeM" placeholder="请选择"  allow-clear>
                <a-select-option value="dock_type">拖车港口</a-select-option>
                <a-select-option value="ocean_dock_type">海运港口</a-select-option>
                <a-select-option value="sealedLand">加封地</a-select-option>
                <a-select-option value="box_type">箱型</a-select-option>
                <a-select-option value="ship">船公司</a-select-option>
                <a-select-option value="applyLand">报关地</a-select-option>
                <a-select-option value="oil_price">油价</a-select-option>
                <a-select-option value="exchange_rate">汇率</a-select-option>
                <a-select-option value="maintainFeeAttach">附加费维护</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24" >
            <a-form-item :wrapper-col="{ span: 24 }">
              <em></em>
              <a-space>
                <a-button type="primary" @click="settingsReload">查询</a-button>
                <a-button @click="settingsReset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <!-- 表格 -->
      <ele-pro-table ref="settingsTableRef" row-key="id" :columns="columns" :datasource="datasource"
        :scroll="{ x: 1000 }">
        <template #toolbar>
          <a-space>
            <a-button type="primary" @click="settingsOpenEdit()" v-any-permission="['order:settings:save']">
              <template #icon>
                <plus-outlined />
              </template>
              <span>新建</span>
            </a-button>
          </a-space>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'state'">
            {{ record.state == 'normal' ? '正常' : '停用' }}
          </template>
          <template v-if="column.key === 'typeM'">
            {{ getTypeM(record.typeM) }}
          </template>
          <template v-if="column.key === 'action'">
            <a-space>
              <a @click="settingsOpenEdit(record)" v-any-permission="['order:settings:update']">修改</a>
              <a-divider type="vertical" />
              <a-popconfirm title="确定要删除此项吗？" @confirm="settingsRemove(record)">
                <a class="ele-text-danger" v-any-permission="['order:settings:remove']">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </ele-pro-table>
    </a-card>
    <!-- 设置项添加、修改弹窗 -->
    <ele-modal :width="460" v-model:visible="settingsVisible" :confirm-loading="settingsLoading"
      :title="settingsIsUpdate ? '修改设置项' : '新建设置项'" :body-style="{ paddingBottom: '8px' }" @ok="settingsSave"
      :maskClosable="false">
      <a-form ref="settingsFormRef" :model="settingsForm" :rules="settingsRules"
        :label-col="{ md: { span: 7 }, sm: { span: 24 } }" :wrapper-col="{ md: { span: 17 }, sm: { span: 24 } }">
        <a-form-item label="名称" name="name">
          <a-input allow-clear :maxlength="200" placeholder="请输入名称" v-model:value="settingsForm.name"
            autocomplete="off" />
        </a-form-item>
        <a-form-item label="英文名字" name="englishName">
          <a-input allow-clear :maxlength="200" placeholder="请输入英文名字" v-model:value="settingsForm.englishName"
            autocomplete="off" />
        </a-form-item>
        <a-form-item label="值" name="valueM">
          <a-input allow-clear :maxlength="200" placeholder="请输入值" v-model:value="settingsForm.valueM"
            autocomplete="off" />
        </a-form-item>
        <a-form-item label="经纬度" name="lonLa">
          <a-input allow-clear :maxlength="200" placeholder="请输入经纬度" v-model:value="settingsForm.lonLa"
            autocomplete="off" />
        </a-form-item>
        <a-form-item label="类型" name="typeM">
          <a-select v-model:value="settingsForm.typeM" placeholder="请选择" allow-clear>
            <a-select-option value="dock_type">拖车港口</a-select-option>
                <a-select-option value="ocean_dock_type">海运港口</a-select-option>
            <a-select-option value="sealedLand">加封地</a-select-option>
            <a-select-option value="box_type">箱型</a-select-option>
            <a-select-option value="ship">船公司</a-select-option>
            <a-select-option value="applyLand">报关地</a-select-option>
            <a-select-option value="oil_price">油价</a-select-option>
            <a-select-option value="exchange_rate">汇率</a-select-option>
            <a-select-option value="maintainFeeAttach">附加费维护</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="状态" name="state">
          <a-select v-model:value="settingsForm.state" placeholder="请选择" allow-clear>
            <a-select-option value="normal">正常</a-select-option>
            <a-select-option value="stop">停用</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="备注" name="remark">
          <a-input allow-clear :maxlength="200" placeholder="请输入备注" v-model:value="settingsForm.remark"
            autocomplete="off" />
        </a-form-item>
      </a-form>
    </ele-modal>
  </div>
</template>

<script setup>
import { ref, reactive, watch } from 'vue';
import { toDateString } from 'ele-admin-pro';
import useFormData from '@/utils/use-form-data';
import { message } from 'ant-design-vue';
import { TIME_OUT } from '@/config/damon';

import { pageSettings, removeSettings, addSettings, updateSettings, listSettingsSel } from '@/api/order/settings';

// 表格实例
const settingsTableRef = ref(null);

// 表格列配置
const columns = ref([
  {
    key: 'index',
    width: 48,
    align: 'center',
    fixed: 'left',
    hideInSetting: true,
    customRender: ({ index }) => index + (settingsTableRef.value?.tableIndex ?? 0)
  },
  {
    title: '名称',
    dataIndex: 'name',
    key: 'name',
    // sorter: true,
  },
  {
    title: '英文名字',
    dataIndex: 'englishName',
    key: 'englishName',
    // sorter: true,
  },
  {
    title: '值',
    dataIndex: 'valueM',
    key: 'valueM',
    // sorter: true,
  },
  {
    title: '经纬度',
    dataIndex: 'lonLa',
    key: 'lonLa',
    // sorter: true,
  },
  {
    title: '类型',
    dataIndex: 'typeM',
    key: 'typeM',
    // sorter: true,
  },
  {
    title: '状态',
    dataIndex: 'state',
    key: 'state',
    // sorter: true,
  },
  {
    title: '备注',
    dataIndex: 'remark',
    key: 'remark',
    // sorter: true,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    key: 'createTime',
    // sorter: true,
    resizable: true,
    width: 150,
    ellipsis: true,
    customRender: ({ text }) => toDateString(text)
  },
  {
    title: '创建人',
    dataIndex: 'createName',
    key: 'createName',
    // sorter: true,
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
    key: 'updateTime',
    // sorter: true,
    resizable: true,
    width: 150,
    ellipsis: true,
    customRender: ({ text }) => toDateString(text)
  },
  {
    title: '更新人',
    dataIndex: 'updateName',
    key: 'updateName',
    // sorter: true,
  },
  {
    title: '操作',
    key: 'action',
    width: 200,
    align: 'center',
    fixed: 'right',
    hideInSetting: true
  }
]);

// 表单数据
const { form: settingsWhere, resetFields } = useFormData({
  username: '',
  sex: undefined
});

// 表格数据源
const datasource = ({ page, limit, where, orders }) => {
  return pageSettings({ ...where, ...orders, page, limit });
};

/* 搜索 */
const settingsReload = () => {
  settingsTableRef?.value?.reload({ page: 1, where: settingsWhere });
};
const settingsReload2 = () => {
  settingsTableRef?.value?.reload();
};

let where_timeout = 0
watch(() => settingsWhere, (val) => {
  clearTimeout(where_timeout)
  where_timeout = setTimeout(() => {
    settingsReload()
  }, TIME_OUT);
},
{ deep: true }
)

let getTypeM = (val) => {
  if (val == 'dock_type') {
    return '拖车港口'
  } else if (val == 'ocean_dock_type') {
    return '海运港口'
  } else if (val == 'sealedLand') {
    return '加封地'
  } else if (val == 'box_type') {
    return '箱型'
  } else if (val == 'ship') {
    return '船公司'
  } else if (val == 'applyLand') {
    return '报关地'
  } else if (val == 'oil_price') {
    return '油价'
  } else if (val == 'exchange_rate') {
    return '汇率'
  } else if (val == 'maintainFeeAttach') {
    return '附加费维护'
  } else {
    return ''
  }
}
/*  重置 */
const settingsReset = () => {
  resetFields();
  settingsReload();
};

/* 删除单个 */
const settingsRemove = (record) => {
  const hide = message.loading('请求中..', 0);
  removeSettings(record.id).then((msg) => {
    hide();
    message.success(msg);
    settingsReload2();
  }).catch((e) => {
    hide();
    message.error(e.message);
  });
};

// 当前选中数据
const settingsCurrent = ref(null);

// 弹窗是否打开
const settingsVisible = ref(false);

// 是否是修改
const settingsIsUpdate = ref(false);

// 提交状态
const settingsLoading = ref(false);

//
const settingsFormRef = ref(null);

// 表单数据
const { form: settingsForm, resetFields: settingsResetFormFields } = useFormData({
  id: undefined,
  customer: '',
  vessel: '',
});

// 表单验证规则
const settingsRules = reactive({
  name: [
    {
      required: true,
      message: '请输入名称',
      type: 'string',
    }
  ],
  typeM: [
    {
      required: true,
      message: '请输入类型',
      type: 'string',
    }
  ],
  valueM: [
    {
      required: true,
      message: '请输入值',
      type: 'string',
    }
  ],
  state: [
    {
      required: true,
      message: '请输入状态',
      type: 'string',
    }
  ],
});

/* 打开编辑弹窗 */
const settingsOpenEdit = (record) => {
  settingsResetFormFields();
  settingsFormRef.value?.clearValidate();
  settingsCurrent.value = record?? {};
  settingsVisible.value = true;
  Object.assign(settingsForm, record ?? {});
  settingsIsUpdate.value = !!record;
  if (record == undefined) {
    settingsForm.state = "normal"
    settingsForm.typeM = "dock_type"
  }
};

/* 保存编辑 */
const settingsSave = () => {
  if (!settingsFormRef.value) {
    return;
  }
  settingsFormRef.value.validate().then(() => {
    settingsLoading.value = true;
    const saveOrUpdate = settingsIsUpdate.value ? updateSettings : addSettings;
    saveOrUpdate(settingsForm).then((msg) => {
      settingsLoading.value = false;
      message.success(msg);
      settingsVisible.value = false;
      settingsReload2();
    }).catch((e) => {
      settingsLoading.value = false;
      message.error(e.message);
    });
  }).catch(() => {
  });
};
</script>

<script>
export default {
  name: 'DemoTest'
};
</script>

